﻿using System;
using System.Windows.Input;
using System.Windows.Forms;
using MyWindowsMediaPlayer.Models.Library;
using MyWindowsMediaPlayer.Libs.TreeView;
using MyWindowsMediaPlayer.Libs.Mediator;
using MyWindowsMediaPlayer.Libs.ViewModel;

namespace MyWindowsMediaPlayer.ViewModels.Library
{
	public class LibraryItemViewModel : TreeViewItemViewModel
	{
		# region members

		readonly LibraryModel _item;
		ICommand _selectCommand;
		Mediator _mediator;

		#endregion

		#region Constructor

		public LibraryItemViewModel ( LibraryModel item, Mediator mediator )
			: base(null, true)
		{
			this._item = item;
			this._mediator = mediator;
		}

		#endregion

		#region Accessors

		public String Name
		{
			get { return this._item.Name; }
		}

		protected override void LoadChildren ( )
		{
			LibraryModel[] Childs = LibraryDatabase.initSubElem(this._item);
			if (Childs != null)
				foreach (LibraryModel Child in Childs)
					base.Children.Add(new LibrarySubItemViewModel(Child, this._mediator));
		}
		
		public ICommand SelectCommand
		{
			get 
			{
				if (this._selectCommand == null)
					this._selectCommand = new RelayCommand(( ) => this.SelectItem());
				return this._selectCommand;
			}
		}

		#endregion

		#region Command

		public void SelectItem ( )
		{
			this._mediator.NotifyColleagues(MediatorMessage.LibraryItemSelected, this._item.Choice);
		}

		#endregion
	}
}
